package com.jboost.modules.app.service.strategry.hs;


import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Maps;
import com.google.gson.JsonObject;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.BaseResult;
import com.jboost.modules.app.dto.HxPriceDto;
import com.jboost.modules.system.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service(value = "HsPriceCountQuery")
@Slf4j
public class HsPriceCountQuery extends AbstractHsMateria<JsonObject, JsonObject> {


    public String getAppId() {
        return "648413a12b740b0008c630a7";
    }

    public String getFormId() {
        return "643cbd5bd7e01f000ad1973f";
    }

    public static void main(String[] args) throws IOException {
    }
    @Async
    @Scheduled(cron = "0/5 * * * * ?")
    public BaseResult invokeStrategy() throws Exception {
        log.info("开始批量查询统计数据数据");
        HashMap<Object, Object> param = Maps.newHashMap();
        param.put("limit",100);
        String s = jdyQueryAll(param);
        HashMap hashMap = JSONUtil.fromJson(s, HashMap.class);
        Object data = hashMap.get("data");
        List<HxPriceDto.DataBean> hxPriceDtos = JSONArray.parseArray(JSONUtil.toJson(data), HxPriceDto.DataBean.class);
        hxPriceDtos.forEach(e->{
            String s2 = redisTemplate.opsForValue().get("HxPriceDto" + e.get_id());
            if (StringUtils.isBlank(s2)) {
                HxPriceDto.DataBean.PriceReq transfer = transfer(e);

                try {
                    String s1 = jdyUpdate(e.get_id(), transfer);
                    Thread.sleep(2000);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                redisTemplate.opsForValue().set("HxPriceDto" + e.get_id(),e.get_id());
            }
        });
        return BaseResult.success();
    }

    private HxPriceDto.DataBean.PriceReq transfer(HxPriceDto.DataBean hxPriceDto) {
        HxPriceDto.DataBean.PriceReq priceReq = new HxPriceDto.DataBean.PriceReq();
        List<HxPriceDto.DataBean.Widget1679571598247Bean> widget1679571598247 = hxPriceDto.get_widget_1679571598247();
        priceReq.setBm(getParaMap(bm(widget1679571598247)));
        priceReq.setCpm(getParaMap(cpm(widget1679571598247)));
        priceReq.setHb(getParaMap(hb(widget1679571598247)));
        priceReq.setMj(getParaMap(mj(widget1679571598247)));
        priceReq.setPq(getParaMap(pq(widget1679571598247)));
        priceReq.setMw(getParaMap(mw(widget1679571598247)));
        priceReq.setHx(getParaMap(hx(widget1679571598247)));
        return priceReq;
    }




    private Object hx(List<HxPriceDto.DataBean.Widget1679571598247Bean> widget1679571598247) {
        AtomicInteger i = new AtomicInteger(1);
        return mergeHX(widget1679571598247).stream().map(e->{
            HxPriceDto.DataBean.HxBean1 cpmBean = new HxPriceDto.DataBean.HxBean1();
            cpmBean.setHx1(getParaMap(i.get()));
            i.getAndIncrement();
            cpmBean.setHx2(getParaMap(e.getF10()));
            cpmBean.setHx3(getParaMap(e.getF4()));
            return cpmBean;
        }).collect(Collectors.toList());
    }

    public static List<HxPriceDto.DataBean.Widget1679571598247Bean> mergeHX(List<HxPriceDto.DataBean.Widget1679571598247Bean> list) {
        List<HxPriceDto.DataBean.Widget1679571598247Bean> result = list.stream()
                // 表示id为key， 接着如果有重复的，那么从BillsNums对象o1与o2中筛选出一个，这里选择o1，
                // 并把id重复，需要将nums和sums与o1进行合并的o2, 赋值给o1，最后返回o1
                //3 4 5
                .collect(Collectors.toMap(HxPriceDto.DataBean.Widget1679571598247Bean::getF10, a -> a, (o1, o2)-> {
                    o1.setF4(o1.getF4() + o2.getF4());
                    return o1;
                })).values().stream().collect(Collectors.toList());
        return result ;
    }

    private Object mw(List<HxPriceDto.DataBean.Widget1679571598247Bean> widget1679571598247) {
        AtomicInteger i = new AtomicInteger(1);
        return mergeMW(widget1679571598247).stream().map(e->{
            HxPriceDto.DataBean.MwBean1 cpmBean = new HxPriceDto.DataBean.MwBean1();
            cpmBean.setMw1(getParaMap(i.get()));
            i.getAndIncrement();
            cpmBean.setMw2(getParaMap(e.getF9()));
            cpmBean.setMw3(getParaMap(e.getF4()));
            return cpmBean;
        }).collect(Collectors.toList());
    }

    public static List<HxPriceDto.DataBean.Widget1679571598247Bean> mergeMW(List<HxPriceDto.DataBean.Widget1679571598247Bean> list) {
        List<HxPriceDto.DataBean.Widget1679571598247Bean> result = list.stream()
                // 表示id为key， 接着如果有重复的，那么从BillsNums对象o1与o2中筛选出一个，这里选择o1，
                // 并把id重复，需要将nums和sums与o1进行合并的o2, 赋值给o1，最后返回o1
                //3 4 5
                .collect(Collectors.toMap(HxPriceDto.DataBean.Widget1679571598247Bean::getF9, a -> a, (o1, o2)-> {
                    o1.setF4(o1.getF4() + o2.getF4());
                    return o1;
                })).values().stream().collect(Collectors.toList());
        return result ;
    }

    private Object pq(List<HxPriceDto.DataBean.Widget1679571598247Bean> widget1679571598247) {
        AtomicInteger i = new AtomicInteger(1);
        return mergePQ(widget1679571598247).stream().map(e->{
            HxPriceDto.DataBean.MjBean1 cpmBean = new HxPriceDto.DataBean.MjBean1();
            cpmBean.setM1(getParaMap(i.get()));
            i.getAndIncrement();
            cpmBean.setM2(getParaMap(e.getF7()));
            cpmBean.setM3(getParaMap(e.getF4()));
            return cpmBean;
        }).collect(Collectors.toList());
    }

    public static List<HxPriceDto.DataBean.Widget1679571598247Bean> mergePQ(List<HxPriceDto.DataBean.Widget1679571598247Bean> list) {
        List<HxPriceDto.DataBean.Widget1679571598247Bean> result = list.stream()
                // 表示id为key， 接着如果有重复的，那么从BillsNums对象o1与o2中筛选出一个，这里选择o1，
                // 并把id重复，需要将nums和sums与o1进行合并的o2, 赋值给o1，最后返回o1
                //3 4 5
                .collect(Collectors.toMap(HxPriceDto.DataBean.Widget1679571598247Bean::getF8, a -> a, (o1, o2)-> {
                    o1.setF4(o1.getF4() + o2.getF4());
                    return o1;
                })).values().stream().collect(Collectors.toList());
        return result ;
    }

    private Object mj(List<HxPriceDto.DataBean.Widget1679571598247Bean> widget1679571598247) {
        AtomicInteger i = new AtomicInteger(1);
        return mergeMJ(widget1679571598247).stream().map(e->{
            HxPriceDto.DataBean.MjBean1 cpmBean = new HxPriceDto.DataBean.MjBean1();
            cpmBean.setM1(getParaMap(i.get()));
            i.getAndIncrement();
            cpmBean.setM2(getParaMap(e.getF7()));
            cpmBean.setM3(getParaMap(e.getF4()));
            return cpmBean;
        }).collect(Collectors.toList());
    }

    public static List<HxPriceDto.DataBean.Widget1679571598247Bean> mergeMJ(List<HxPriceDto.DataBean.Widget1679571598247Bean> list) {
        List<HxPriceDto.DataBean.Widget1679571598247Bean> result = list.stream()
                // 表示id为key， 接着如果有重复的，那么从BillsNums对象o1与o2中筛选出一个，这里选择o1，
                // 并把id重复，需要将nums和sums与o1进行合并的o2, 赋值给o1，最后返回o1
                //3 4 5
                .collect(Collectors.toMap(HxPriceDto.DataBean.Widget1679571598247Bean::getF7, a -> a, (o1, o2)-> {
                    o1.setF4(o1.getF4() + o2.getF4());
                    return o1;
                })).values().stream().collect(Collectors.toList());
        return result ;
    }

    private Object hb(List<HxPriceDto.DataBean.Widget1679571598247Bean> widget1679571598247) {
        AtomicInteger i = new AtomicInteger(1);
        return mergeHb(widget1679571598247).stream().map(e->{
            HxPriceDto.DataBean.HbBean1 cpmBean = new HxPriceDto.DataBean.HbBean1();
            cpmBean.setH1(getParaMap(i.get()));
            i.getAndIncrement();
            cpmBean.setH2(getParaMap(e.getF6()));
            cpmBean.setH3(getParaMap(e.getF4()));
            return cpmBean;
        }).collect(Collectors.toList());
    }

    public static List<HxPriceDto.DataBean.Widget1679571598247Bean> mergeHb(List<HxPriceDto.DataBean.Widget1679571598247Bean> list) {
        List<HxPriceDto.DataBean.Widget1679571598247Bean> result = list.stream()
                // 表示id为key， 接着如果有重复的，那么从BillsNums对象o1与o2中筛选出一个，这里选择o1，
                // 并把id重复，需要将nums和sums与o1进行合并的o2, 赋值给o1，最后返回o1
                //3 4 5
                .collect(Collectors.toMap(HxPriceDto.DataBean.Widget1679571598247Bean::getF6, a -> a, (o1, o2)-> {
                    o1.setF4(o1.getF4() + o2.getF4());
                    return o1;
                })).values().stream().collect(Collectors.toList());
        return result ;
    }

    private Object cpm(List<HxPriceDto.DataBean.Widget1679571598247Bean> widget1679571598247) {
        AtomicInteger i = new AtomicInteger(1);
        return mergeCpm(widget1679571598247).stream().map(e->{
            HxPriceDto.DataBean.CpmBean1 cpmBean = new HxPriceDto.DataBean.CpmBean1();
            cpmBean.setC1(getParaMap(i.get()));
            i.getAndIncrement();
            cpmBean.setC2(getParaMap(e.getF2()));
            cpmBean.setC3(getParaMap(e.getF4()));
            return cpmBean;
        }).collect(Collectors.toList());
    }

    public static List<HxPriceDto.DataBean.Widget1679571598247Bean> mergeCpm(List<HxPriceDto.DataBean.Widget1679571598247Bean> list) {
        List<HxPriceDto.DataBean.Widget1679571598247Bean> result = list.stream()
                // 表示id为key， 接着如果有重复的，那么从BillsNums对象o1与o2中筛选出一个，这里选择o1，
                // 并把id重复，需要将nums和sums与o1进行合并的o2, 赋值给o1，最后返回o1
                //3 4 5
                .collect(Collectors.toMap(HxPriceDto.DataBean.Widget1679571598247Bean::getF2, a -> a, (o1, o2)-> {
                    o1.setF4(o1.getF4() + o2.getF4());
                    return o1;
                })).values().stream().collect(Collectors.toList());
        return result ;
    }

    private Object bm(List<HxPriceDto.DataBean.Widget1679571598247Bean> widget1679571598247) {
        AtomicInteger i = new AtomicInteger(1);
        List<HxPriceDto.DataBean.Widget1679571598247Bean> widget1679571598247Beans = mergeBm(widget1679571598247);
        return widget1679571598247Beans.stream().map(e->{
            HxPriceDto.DataBean.BmBean1 bmBean = new HxPriceDto.DataBean.BmBean1();
            bmBean.setB1(getParaMap(i.get()));
            i.getAndIncrement();
            bmBean.setB2(getParaMap(e.getF1()));
            bmBean.setB3(getParaMap(e.getF3()));
            bmBean.setB4(getParaMap(e.getF4()));
            bmBean.setB5(getParaMap(e.getF5()));
            return bmBean;

        }).collect(Collectors.toList());
    }

    public static List<HxPriceDto.DataBean.Widget1679571598247Bean> mergeBm(List<HxPriceDto.DataBean.Widget1679571598247Bean> list) {
        List<HxPriceDto.DataBean.Widget1679571598247Bean> result = list.stream()
                // 表示id为key， 接着如果有重复的，那么从BillsNums对象o1与o2中筛选出一个，这里选择o1，
                // 并把id重复，需要将nums和sums与o1进行合并的o2, 赋值给o1，最后返回o1
                //3 4 5
                .collect(Collectors.toMap(HxPriceDto.DataBean.Widget1679571598247Bean::getF1, a -> a, (o1, o2)-> {
                    o1.setF3(o1.getF3()+o2.getF3());
                    o1.setF4(o1.getF4() + o2.getF4());
                    o1.setF5(o1.getF5()+o2.getF5());
                    return o1;
                })).values().stream().collect(Collectors.toList());
        return result ;
    }
}
